package com.company.algo.listnode;

import com.company.algo.pojo.ListNode;

public class RemoveNthFromEnd {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode dummy = new ListNode(-1);
        dummy.next = head;
        ListNode p1 = dummy;
        ListNode p2 = dummy;
        for(int i = 0; i < n+1; i++){
            if(p1==null) throw new RuntimeException("error input");
            p1 = p1.next;
        }
        while(p1!=null){
            p1 = p1.next;
            p2 = p2.next;
        }
        assert p2 != null;
        p2.next = p2.next.next;
        return dummy.next;
    }
}
